//
//  YYSimpleWebViewController.m
//  YYKitExample
//
//  Created by ibireme on 15/9/11.
//  Copyright (c) 2015 ibireme. All rights reserved.
//

#import "YYSimpleWebViewController.h"
#import "YYKit.h"
#import <WebKit/WebKit.h>

@interface YYSimpleWebViewController () <WKUIDelegate, WKNavigationDelegate>
//@property (nonatomic, strong) UIWebView *webView;
@property (strong, nonatomic) WKWebView *wkWebView;
@property (nonatomic, strong) NSURL *url;
@end

@implementation YYSimpleWebViewController

- (instancetype)initWithURL:(NSURL *)url {
    self = [super init];
    _url = url;
//    _webView = [UIWebView new];
//    _webView.delegate = self;
    _wkWebView = [[WKWebView alloc]initWithFrame:CGRectZero];
    _wkWebView.navigationDelegate = self;
    _wkWebView.UIDelegate = self;
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
//    _webView.frame = self.view.bounds;
//    if (kSystemVersion < 7) _webView.height -= 44;
    _wkWebView.frame = self.view.bounds;
    if (kSystemVersion < 7) _wkWebView.height -= 44;
    [self.view addSubview:_wkWebView];
    
//    [_webView loadRequest:[NSURLRequest requestWithURL:_url]];
    [_wkWebView loadRequest:[NSURLRequest requestWithURL:_url]];
}

//- (void)webViewDidFinishLoad:(UIWebView *)webView {
//     self.title = [webView stringByEvaluatingJavaScriptFromString:@"document.title"];
//}

- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation
{
//    self.title = [webView stringByEvaluatingJavaScriptFromString:@"document.title"];
    [webView evaluateJavaScript:@"document.title" completionHandler:^(id _Nullable title, NSError * _Nullable error) {
        if (!error && [title isKindOfClass:[NSString class]]) {
            self.title = title;
        }
    }];
}

@end
